package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.godmch.parameter.getAgentRateListBean;
import com.ymatou.iapi.godmch.parameter.setAgentRateBean;
import com.ymatou.iapi.godmch.parameter.setAgentRateBean.agentRate;
import com.ymatou.iapi.godmch.service.getAgentRateListCall;
import com.ymatou.iapi.godmch.service.setAgentRateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
/**
*代理类型费率列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentRateList{
	private static getAgentRateListBean bean;
	private static getAgentRateListCall call;
	public agent_typeWapper agentTypeWapper = new agent_typeWapper();
	
	private final static Integer channlNoAgent=500400810;//渠道无代理
	private final static Integer channlAndAgent=500400812;//渠道有代理
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理类型费率列表");
	}

	@Before
	public void caseUp() {
		bean=new getAgentRateListBean();
		call=new getAgentRateListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRateList_001() {
		Logger.start(true,"正例-代理类型费率列表-有代理商");
		try {
			
			bean.setMchId(channlAndAgent);
			call.setData(bean);
			call.callService();		
			
			// 获取代理类型
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlAndAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");	
			
			JSONArray jsons=call.getAgentRateList();
			for(int i=0;i<jsons.length();i++)
			{
				Logger.verifyEquals(list.get(i).getAgentType(), Integer.parseInt(new JSONObject(jsons.get(i).toString()).get("agentType").toString()), "验证第"+(i+1)+"条数据的agentType");
				Logger.verifyEquals(list.get(i).getTypeTitle(), new JSONObject(jsons.get(i).toString()).get("title").toString(), "验证第"+(i+1)+"条数据的title");
				Logger.verifyEquals(list.get(i).getRate().stripTrailingZeros().toPlainString(), new BigDecimal(new JSONObject(jsons.get(i).toString()).get("agentRate").toString()).stripTrailingZeros().toPlainString(), "验证第"+(i+1)+"条数据的agentRate");
				
				if(i==0)
					Logger.verifyEquals(1, Integer.parseInt(new JSONObject(jsons.get(i).toString()).get("useCount").toString()), "验证第"+(i+1)+"条数据的useCount");
				else {
					Logger.verifyEquals(0, Integer.parseInt(new JSONObject(jsons.get(i).toString()).get("useCount").toString()), "验证第"+(i+1)+"条数据的useCount");
				}
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRateList_002() {
		Logger.start(true,"正例-代理类型费率列表-无代理商");
		try {
			
			bean.setMchId(channlNoAgent);
			call.setData(bean);
			call.callService();		
			
			// 获取代理类型
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlNoAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");	
			
			JSONArray jsons=call.getAgentRateList();
			for(int i=0;i<jsons.length();i++)
			{
				Logger.verifyEquals(list.get(i).getAgentType(), Integer.parseInt(new JSONObject(jsons.get(i).toString()).get("agentType").toString()), "验证第"+(i+1)+"条数据的agentType");
				Logger.verifyEquals(list.get(i).getTypeTitle(), new JSONObject(jsons.get(i).toString()).get("title").toString(), "验证第"+(i+1)+"条数据的title");
				Logger.verifyEquals(list.get(i).getRate().stripTrailingZeros().toPlainString(), new BigDecimal(new JSONObject(jsons.get(i).toString()).get("agentRate").toString()).stripTrailingZeros().toPlainString(), "验证第"+(i+1)+"条数据的agentRate");
				
				Logger.verifyEquals(0, Integer.parseInt(new JSONObject(jsons.get(i).toString()).get("useCount").toString()), "验证第"+(i+1)+"条数据的useCount");

			}			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRateList_003() {
		Logger.start(true,"正例-代理类型费率列表-代理类型费率为0");
		try {
			setAgentRateBean setAgentRateBean=new setAgentRateBean();
			setAgentRateCall setAgentRateCall=new setAgentRateCall();
			// 获取代理类型
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlNoAgent);
			agentRate rate= (new setAgentRateBean()).new agentRate();
			List<agentRate> agentRates=new ArrayList();
			
			List<agent_type> list = agentTypeWapper.selectby(map);
			rate.setAgentType(list.get(0).getAgentType());
			rate.setAgentRate(0);
			agentRates.add(rate);
			
			setAgentRateBean.setMchId(channlNoAgent);
			setAgentRateBean.setAgentRateList(agentRates);
			setAgentRateCall.setData(setAgentRateBean);
			setAgentRateCall.callService();	
			
			bean.setMchId(channlNoAgent);
			call.setData(bean);
			call.callService();		
			
			// 获取代理类型
			 list = agentTypeWapper.selectby(map);
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");	
			
			JSONArray jsons=call.getAgentRateList();
			for(int i=0;i<jsons.length();i++)
			{
				Logger.verifyEquals(list.get(i).getAgentType(), Integer.parseInt(new JSONObject(jsons.get(i).toString()).get("agentType").toString()), "验证第"+(i+1)+"条数据的agentType");
				Logger.verifyEquals(list.get(i).getTypeTitle(), new JSONObject(jsons.get(i).toString()).get("title").toString(), "验证第"+(i+1)+"条数据的title");
				Logger.verifyEquals(list.get(i).getRate().stripTrailingZeros().toPlainString(), new BigDecimal(new JSONObject(jsons.get(i).toString()).get("agentRate").toString()).stripTrailingZeros().toPlainString(), "验证第"+(i+1)+"条数据的agentRate");
				
				Logger.verifyEquals(0, Integer.parseInt(new JSONObject(jsons.get(i).toString()).get("useCount").toString()), "验证第"+(i+1)+"条数据的useCount");

			}				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getAgentRateList_004() {
		Logger.start(true,"正例-代理类型费率列表-代理类型有代理商使用(case1包含)");
		try {
			
			call.setData(bean);
			call.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentRateList_005() {
		Logger.start(true,"正例-代理类型费率列表-代理类型有代理商无使用(case2包含)");
		try {
			
			call.setData(bean);
			call.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}